import {
  apiUrl
} from '@/api/request'
import {
  bannerList
} from '@/api/home'
import {
  getgemomemberuser, getmemberinfo, getMemberLevel,getMyActivityList
} from '@/api/my'
import { showText } from "../../utils/util";
import { getBookingInfo } from "../../api/spa";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUri: getApp().globalData.imgUri,
    cdn: getApp().globalData.cdn,
    showPopup: false,
    avatarUrl: getApp().globalData.imgUri + '/my/avatarUrl.png',
    nickName: 'GEMO会员',
    memberPoints: 0,
    currentLevel: '',
    nextLevel: '',
    nextLevelPoints: 0,
    bannerListData: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getgemomemberuserFun()
  },
  getgemomemberuserFun() {
    getgemomemberuser({}).then(res => {
      var avatarUrl = res.data.avatarUrl;
      var nickName = res.data.nickName;
      if (avatarUrl) {
        this.setData({
          avatarUrl: getApp().globalData.cdn + avatarUrl
        })
      }
      if (nickName) {
        this.setData({
          nickName: nickName
        });
      }

      getmemberinfo({}).then(res => {
        if (res.code == 200) {
          this.setData({
            memberPoints: res.data.shopperInfo.memberPoints
          });
          getMemberLevel({}).then(res => {
            if (res.code == 200) {
              this.setData({
                currentLevel: res.data.currentLevel,
                nextLevel: res.data.nextLevel,
                nextLevelPoints: res.data.nextLevelPoints,
              });

              let phone = wx.getStorageSync('phone')
              let o = {
                moduleType: 'MORE_ACTIVITY',
                status: 0,
              }
              if (phone) {
                o.gemoLevel = wx.getStorageSync('memberLevel').currentLevelEnum
                o.haveCommonBanner = true
              } else {
                o.onlyCommonBanner = true
              }
              bannerList(o).then(res => {
                if (res.code == 200) {
                  this.setData({
                    bannerListData: res.rows
                  })
                }
              })
            }
          });
        }
      });


    });
  },
  openAuthPopup() {
    this.setData({
      showPopup: true
    })
  },
  goToBase() {
    wx.navigateTo({
      url: '/pages/base/base',
    })
  },
  closeAuth(e) {
    console.log(e)
    this.setData({
      showPopup: false
    })
    if (e.detail) {
      this.getgemomemberuserFun()
    }
  },
  // 设置头像
  setAvatarUrl(e) {
    console.log(e)
    this.setData({
      avatarUrl: e.detail
    })
    let url = apiUrl + '/file/uploadFile'
    // 上传图片
    wx.uploadFile({
      filePath: e.detail,
      name: 'file',
      header: {
        'content-type': 'application/json',
        'Authorization': wx.getStorageSync('access_token')
      },
      url: url,
      success: (res) => {
        let res1 = JSON.parse(res.data)
        console.log(res1.data.url)
        this.setData({
          avatarUrl: getApp().globalData.cdn + res1.data.url
        })
      },
      fail: (err) => {
        console.log('Url:', url, ' err:', err)
      }
    })
  },
  goToEquity() {
    wx.navigateTo({
      url: '/pages/memberRights/memberRights',
    })
    // showText('您好，该活动暂未开始，敬请期待！')
  },
  goToSpa() {
    getBookingInfo({}).then(res => {
      if (res.code == 200) {
        var data = res.data;
        var mobile = data.bookingInfo.mobile;
        var bookingId = data.bookingInfo.id;
        var absId = data.bookingInfo.shopId;
        var spaId = data.bookingInfo.spaId;
        if (mobile) {
          wx.navigateTo({
            url: '/pages/bookSuccess/bookSuccess',
          })
        } else {
          wx.navigateTo({
            url: '/pages/book/book?bookingId=' + bookingId + '&absId=' + absId + '&spaId=' + spaId,
          })
        }
      } else {
        showText('您尚未预约spa体验');
      }
    });
  },
  goToUrl(e){
    let { index } = e.currentTarget.dataset
    let detail = this.data.bannerListData[index]
    console.log(detail)
    if (detail.jumpType == "INNER_APP") { //内部小程序
      wx.navigateTo({
        url: detail.jumpParam,
      });
    } else if (detail.jumpType == "OUT_APP") { //外部小程序
      wx.navigateToMiniProgram({
        appId: detail.jumpAppid,
        path: detail.jumpParam
      });
    } else if (detail.jumpType == "WEB_H5") { //
      wx.navigateTo({
        url: '/shop/webview/webview?webSrc=' + detail.jumpParam,
      })
    }
  },
  goToClub() {
    let o = {pageNum:1,pageSize:10}
    getMyActivityList(o).then(res => {
      if(res.code == 200){
        if(res.data.listData.length > 0){
          wx.navigateTo({
            url: '/pages/myClub/myClub',
          })
        }else{
          showText('您尚未预约俱乐部活动');
        }
      }
    });
  },
  goToOrder() {
    // showText('您好，该活动暂未开始，敬请期待！')
    wx.navigateTo({
      url: '/pages/myOrder/myOrder',
    })
  },
  onShareAppMessage() {
    return {
      title: getApp().globalData.share_title,
      path: getApp().globalData.share_url,
      imageUrl: getApp().globalData.share_image,
    }
  }
})